package com.nowcoder.community.controller.admin;

import com.alibaba.fastjson.JSONObject;
import com.nowcoder.community.service.UserService;
import com.nowcoder.community.util.RedisKeyUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

import static com.nowcoder.community.util.CommunityConstant.DEFAULT_EXPIRED_SECONDS;
import static com.nowcoder.community.util.CommunityConstant.REMEMBER_EXPIRED_SECONDS;

/**
 * 注释
 *
 * @author Coutana
 * @since 2.9.0
 */
@Controller
public class AdminController {
    @Value("${server.servlet.context-path}")
    private String contextPath;
    private final RedisTemplate<String,Object> redisTemplate;
    private final UserService userService;
    @Autowired
    public AdminController(RedisTemplate<String, Object> redisTemplate, UserService userService) {
        this.redisTemplate = redisTemplate;
        this.userService = userService;
    }

    @RequestMapping(path = "/admin/login",method = RequestMethod.GET)
    public String getAdminLoginPage(String error,Model model) {
        // 错误信息
        // JSON error={"username":"","password":"","captcha":""}
        if(error!=null&&!error.isEmpty()){
            model.addAttribute("error",error);
        }
        return "site/admin/login";
    }

    @ResponseBody
    @PostMapping(value = "/admin/login")
    public String adminLogin(String username,String password,String captcha,boolean remember,@CookieValue(name = "kaptchaOwner") String captchaId) {
        // 检查验证码
        // String kaptcha = (String) session.getAttribute("kaptcha");
        String kaptcha = null;

        JSONObject res= new JSONObject();

        if (StringUtils.isNotBlank(captchaId)) {
            kaptcha = (String) redisTemplate.opsForValue().get(RedisKeyUtil.getKaptchaKey(captchaId));
        }

        if (StringUtils.isBlank(kaptcha) || StringUtils.isBlank(captcha) || !kaptcha.equalsIgnoreCase(captcha)) {

            return new JSONObject()
                    .fluentPut("code",400)
                    .fluentPut("error",res.fluentPut("captcha", "验证码不正确!")).toJSONString();
        }

        // 检查账号,密码
        int expiredSeconds = remember ? REMEMBER_EXPIRED_SECONDS : DEFAULT_EXPIRED_SECONDS;
        Map<String, Object> map = userService.login(username, password, expiredSeconds);
        if (!map.containsKey("ticket")){
            return new JSONObject()
                    .fluentPut("code",400)
                    .fluentPut("error",res
                            .fluentPut("username",map.get("usernameMsg"))
                            .fluentPut("password",map.get("passwordMsg"))).toJSONString();
        }

        return new JSONObject()
                .fluentPut("code",200)
                .fluentPut("data",res
                        .fluentPut("ticket",map.get("ticket"))
                        .fluentPut("expired",expiredSeconds)
                        .fluentPut("redirect","/admin/index")).toJSONString();
    }

}
